<?
    class MailManager {
        private static $_mailer = null;
        
        static function MailEnabled() {
            return SettingManager::GetSetting("outgoing_mail_enabled");
        }
        
        private static function reInit() {
            if (self::MailEnabled()) {
                require_once("mail/phpmailer.php");
                $mailoptions = new SettingManager("outgoing mail");
                self::$_mailer = new PHPMailer();
                
                self::$_mailer->IsSMTP();
                self::$_mailer->SMTPAuth   = (!empty($mailoptions->Username) && !empty($mailoptions->Password));        // enable SMTP authentication if we should
                self::$_mailer->SMTPSecure = ($mailoptions->encryption == "none" ? "" : $mailoptions->encryption);      // sets the prefix to the servier
                self::$_mailer->Host       = $mailoptions->server;                                                      // sets the SMTP server
                self::$_mailer->Port       = $mailoptions->port;                                                        // set the SMTP port 

                if (self::$_mailer->SMTPAuth) {
                    self::$_mailer->Username   = $mailoptions->username;                                 // username
                    self::$_mailer->Password   = $mailoptions->password;                                 // password
                }
                self::$_mailer->From       = $mailoptions->from_address;
                self::$_mailer->FromName   = SettingManager::GetSetting("application_title");

                self::$_mailer->Subject    = "(Untitled E-Mail From Offcampus Housing)";
                self::$_mailer->MsgHTML($body);
                self::$_mailer->IsHTML(true); // send as HTML
                
                self::$_mailer->AddReplyTo($mailoptions->from_address, SettingManager::GetSetting("application_title"));
            }
            else self::$_mailer = null;
        }
        static function SendMail($to,$name,$subject,$message,$plaintext = null) {
            self::reInit(); // make sure there's a fresh object to send with
            
            if (self::$_mailer != null) {
                self::$_mailer->AddAddress($to,$name);
                self::$_mailer->Subject = $subject;
                self::$_mailer->MsgHTML($message);
                
                if ($plaintext)
                    self::$_mailer->AltBody = $message;
                
                if (!self::$_mailer->Send()) {
                    self::Err(); 
                    return false;
                } 
                else return true;
            }
        }
        static function Err() {
            ErrorMessage::Show("Error Sending Mail",self::$_mailer->ErrorInfo);
        }
        static function SendGuestAccountConfirmation($guestid) {
            $guest = DBM::FetchRow("SELECT * FROM guests WHERE id='$guestid';");
            
            // build the message
            $message = SettingManager::GetSetting("Guest accounts confirmation email");
            $message = DynaText::BindFromEnv($message);
            $message = DynaText::BindFromDatabase($message,"guests",$guestid);
            $htmlmessage = DynaText::ParseForMail($message);
            
            // send the message
            return self::SendMail($guest['email'],$guest['name'],"Guest Account Approved",$htmlmessage,$message);
        }
        static function SendGuestAccountRequest($guestid) {
            $guest = DBM::FetchRow("SELECT * FROM guests WHERE id='$guestid';");
            
            $message = SettingManager::GetSetting("Guest accounts request email");
            $message = DynaText::BindFromEnv($message);
            $message = DynaText::BindFromDatabase($message,"guests",$guestid);
            $htmlmessage = DynaText::ParseForMail($message);
            
            
            return self::SendMail(SettingManager::GetSetting("admin email"),'Sysadmin',"Guest Account Requested",$htmlmessage,$message);
        }
        static function SendResetPassword($email, $guestid) {
            $message = SettingManager::GetSetting("guest accounts password reset");
            $message = DynaText::BindFromEnv($message);
            $message = DynaText::BindFromDatabase($message,"guests",$guestid);
            $htmlmessage = DynaText::ParseForMail($message);
            
            return self::SendMail($email,'',SettingManager::GetSetting("application title") . ": Password Reset Request",$htmlmessage,$message);
        }
        static function SendResetPasswordComplete($email, $guestid) {
            $message = SettingManager::GetSetting("guest accounts password hasbeen reset");
            $message = DynaText::BindFromEnv($message);
            $message = DynaText::BindFromDatabase($message,"guests",$guestid);
            $htmlmessage = DynaText::ParseForMail($message);
            
            return self::SendMail($email,'',"Your password has been reset",$htmlmessage,$message);
        }
    }
?>
